package models

import (
	"t11Lib/t11Errors"
	"t11Lib/t11Log"
	"t11Lib/t11Model"
)

type UserDeviceKv struct {
	t11Model.BaseMdl
	UserId string
	DevId  string
	Key    string
	Value  string
}

func (self *UserDeviceKv) InitId() {
	self.Id = self.UserId + self.DevId + self.Key
}

type UserGatewayKv struct {
	t11Model.BaseMdl
	UserId    string
	GatewayId string
	Key       string
	Value     string
}

func (self *UserGatewayKv) InitId() {
	self.Id = self.UserId + self.GatewayId + self.Key
}

type userKvDao struct{}

var UserKvDao userKvDao

func (self userKvDao) GetGatewayKvByUser(conn *t11Model.DbConn, user *JwtToken) ([]*UserGatewayKv, t11Errors.T11Error) {
	var ret []*UserGatewayKv
	_, e := conn.QueryTable(&UserGatewayKv{}).Filter("UserId", user.ToId()).All(&ret)
	return ret, t11Errors.Wrap(e)
}

func (self userKvDao) GetDeviceKvByUser(conn *t11Model.DbConn, user *JwtToken) ([]*UserDeviceKv, t11Errors.T11Error) {
	var ret []*UserDeviceKv
	_, e := conn.QueryTable(&UserDeviceKv{}).Filter("UserId", user.ToId()).All(&ret)
	return ret, t11Errors.Wrap(e)
}

func (self userKvDao) DeleteDeviceKvByUser(conn *t11Model.DbConn, devId string, token *JwtToken) t11Errors.T11Error {
	dev := &UserDeviceKv{}
	dev.UserId = token.ToId()
	dev.DevId = devId
	_, e := conn.Delete(dev, "UserId", "DevId")
	return t11Errors.Wrap(e)
}

func (self userKvDao) DeleteDeviceKvByUserAndKey(conn *t11Model.DbConn, devId string, token *JwtToken, key string) t11Errors.T11Error {
	kv := &UserDeviceKv{}
	kv.UserId = token.ToId()
	kv.Key = key
	kv.DevId = devId
	kv.InitId()
	_, err := conn.Delete(kv)
	return t11Errors.Wrap(err)
}

func (self userKvDao) CreateOrModifyDeviceKv(conn *t11Model.DbConn, user *JwtToken, devId, key, val string) t11Errors.T11Error {
	kv := &UserDeviceKv{}
	kv.UserId = user.ToId()
	kv.DevId = devId
	kv.Key = key
	kv.Value = val
	kv.InitId()
	cnt, e := conn.Update(kv, "Value")
	if cnt != 0 && e == nil {
		return nil
	}
	if e != nil {
		return t11Errors.Wrap(e)
	}
	_, err := conn.Insert(kv)
	return t11Errors.Wrap(err)
}

func (self userKvDao) DeleteGatewayKvByUserAndKey(conn *t11Model.DbConn, user *JwtToken, gwId, key string) t11Errors.T11Error {
	item := &UserGatewayKv{}
	item.UserId = user.ToId()
	item.GatewayId = gwId
	item.Key = key
	item.InitId()
	_, e := conn.Delete(item)
	return t11Errors.Wrap(e)
}

func (self userKvDao) DeleteGatewayKvByGwId(conn *t11Model.DbConn, gwId string) t11Errors.T11Error {
	item := &UserGatewayKv{}
	item.GatewayId = gwId
	_, e := conn.Delete(item, "GatewayId")
	return t11Errors.Wrap(e)
}

func (self userKvDao) CreateOrModifyGatewayKv(conn *t11Model.DbConn, user *JwtToken, gwId, key, val string) t11Errors.T11Error {
	item := &UserGatewayKv{}
	item.UserId = user.ToId()
	item.GatewayId = gwId
	item.Key = key
	item.Value = val
	item.InitId()
	cnt, e := conn.Update(item, "Value")
	if e == nil && cnt != 0 {
		return nil
	}
	if e != nil {
		return t11Errors.Wrap(e)
	}
	_, e2 := conn.Insert(item)
	t11Log.Debuglnf("添加kv完毕，e2 = %v", e2)
	return t11Errors.Wrap(e2)
}

func (self userKvDao) DeleteDeviceKvByDevId(conn *t11Model.DbConn, devId []string) t11Errors.T11Error {
	_, e := conn.QueryTable(&UserDeviceKv{}).Filter("DevId__in", devId).Delete()
	return t11Errors.Wrap(e)
}
